#!/usr/bin/env python
# coding: utf-8

# CTR预估特征工程，数据集可在 https://www.kaggle.com/c/avazu-ctr-prediction 下载

import pandas as pd
import _pickle as pickle

# 读取测试数据
test = pd.read_csv("data/test", dtype={'id': str})

# 保存id
test_id = test['id']
pickle.dump(test_id, open("pkl/test_id.pkl", 'wb'))

# 把编码数据转换成分类代码
for index, col in enumerate(test):
    if (test[col].dtype == "object"):
        test[col] = pd.Categorical(test[col])
        test[col] = test[col].cat.codes

# 处理日期和时间，仅保留时间和weekday
hour_str = test['hour'].astype('str')
test['time'] = pd.to_datetime(hour_str, format="%y%m%d%H")
test['hour'] = test['time'].dt.hour
test['weekday'] = test['time'].dt.weekday
test = test.drop(['time'], axis=1)
# 保存处理结果
test.to_pickle("pkl/test_fe")
test = None
test_id = None

# 训练集也一样处理
train = pd.read_csv('data/train')

for index, col in enumerate(train):
    if (train[col].dtype == "object"):
        train[col] = pd.Categorical(train[col])
        train[col] = train[col].cat.codes

hour_str = train['hour'].astype('str')
train['time'] = pd.to_datetime(hour_str, format="%y%m%d%H")
train['hour'] = train['time'].dt.hour
train['weekday'] = train['time'].dt.weekday
train = train.drop(['time', 'id'], axis=1)
train.to_pickle("pkl/train_fe")
